package u17.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import u17.builder.Builder;
import u17.model.User;
import u17.service.UserService;
import u17.uitls.MD5;


@Controller
@RequestMapping("user")
public class UserController {
	private static final Logger logger = Logger.getLogger(UserController.class);
	
	private String USER_LIST = "userList";  //用户列表页面
	
	private UserService userService;

	public UserService getUserService() {
		return userService;
	}

	@Autowired
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	/*
	 * 获得所有玩家 
	 */
	@RequestMapping("/getAllUser")
	public ModelAndView getAllUser(HttpServletRequest request){
		ModelAndView mav = new ModelAndView(USER_LIST);
		/*
		List<User> users = null;
		try {
			users = userService.getAllUser();
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("UserController类下 getAllUser报错");
		}
		
        Map map = new HashMap();
        map.put("total", users.size());
        //map.put("rows", JSON.toJSONStringWithDateFormat(users, "yyyy-mm-dd"));
        map.put("rows", users);
        System.out.println(JSON.toJSONString(map,true));*/
        
        //request.setAttribute("success", JSON.toJSONString(map));
        //request.setAttribute("users", users);

		//logger.info(JSON.toJSONStringWithDateFormat(users, "yyyy-mm-dd"));
		//request.setAttribute("users", users.toArray(new User[users.size()]));
		return mav;
	}
	
	/*
	 * 修改玩家信息
	 */
	@RequestMapping("/updateUser")
	public ModelAndView updateUser(HttpServletRequest request){
		Builder builder = new Builder(request);
		User user = builder.getUser();
		try {
			userService.updateUser(user);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("UserController类下 updateUser报错");
		}
		return null;
	}
	/*
	 * 修改玩家信息
	 */
	@ResponseBody
	@RequestMapping("/addUser")
	public String addUser(HttpServletRequest request){
		//Builder builder = new Builder(request);
		User user = new User();
		user.setYaccountid("434343");
		user.setYcratedatetime(new Date());
		user.setYlastlogindatetime(new Date());
		user.setYnikename(request.getParameter("nikename"));
		user.setYrealname(request.getParameter("name"));
		user.setYphonenumber(request.getParameter("phone"));
		user.setYpassword(MD5.Md5(request.getParameter("pwd")));
		
		try {
			//判断是否为新添加
			if("0".equals(request.getParameter("yid"))){
				userService.addUser(user);
			}else{
				user.setYid(Integer.valueOf(request.getParameter("yid")));
				userService.updateUser(user);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("UserController类下 addUser报错");
		}
		
		System.out.println("------------------------------------------");
		return "success";
	}
	
	@ResponseBody
	@RequestMapping("/queryUserPage")
	public Map<String,Object> getUserData(HttpServletRequest request){
		Map<String,Object> map = new HashMap<String,Object>();
		
		//1.获取分页传递信息
		int page = Integer.parseInt(request.getParameter("page"));
		int rows = Integer.parseInt(request.getParameter("rows"));

		//2.设置查询条件
		User user = new User();
		if(page > 1){
			user.setFirst_size((page-1)*rows);
		}else{
			user.setFirst_size(page);
		}
		user.setRows(rows);

		//3.执行返回结果
		try {
			map = userService.getUserPaging(user);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("UserController类下 getAllUser报错");
		}
		
		return map;
	}
	
}
